package redis;

import lombok.val;
import org.apache.kafka.common.protocol.types.Field;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import redis.clients.jedis.Jedis;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class jedis_demo {

    Jedis jedis;

    @Before
    public void init(){
        jedis = new Jedis("192.168.0.114", 6379);   //连接redis
    }

    @After
    public void end(){
        jedis.close();  //关闭连接
    }

    @Test
    public void ping() {
        String  ping = jedis.ping();    //测试ping方法
        System.out.println(ping);
    }

    @Test
    public void test1(){
        Set<String> keys = jedis.keys("*"); //获取所有key
        for (String key : keys) {
            System.out.println(key);
        }
    }

    @Test
    public void test_KV(){
        jedis.set("name", "badpig");    //设置k-v
        val name = jedis.get("name"); //获取k对应的v
        System.out.println(name);
        System.out.println("------");
        jedis.mset("k1","v1","k2","v2","k3","v3");//批量设置k-v
        val mget = jedis.mget("k1", "k2", "k3");//批量获取v
        System.out.println(mget);
    }

    @Test
    public void test_list(){ //测试 List
        jedis.lpush("list1","aa","bb","cc");
        List<String > list1 = jedis.lrange("list1", 0, -1);
        System.out.println(list1);
    }

    @Test
    public void test_set(){ //测试set
        jedis.sadd("set1","xm","xh","xm");
        Set<String> set1 = jedis.smembers("set1");
        System.out.println(set1);
    }

    @Test
    public void test_hash(){    //测试hash
        jedis.hset("hashKey1","age","22");
        String age = jedis.hget("hashKey1", "age");
        System.out.println(age);

        System.out.println("--------");

        Map<String,String> map = new HashMap<>();
        map.put("hobby","bicycle");
        jedis.hset("hashKey2",map);
        String hobby = jedis.hget("hashKey2","hobby");
        System.out.println(hobby);
    }

    @Test
    public void test_zset(){ //测试zset
        jedis.zadd("zkey",100d,"China");
        jedis.zadd("zkey",90d,"USA");
        jedis.zadd("zkey",120d,"UK");
        Set<String> counties = jedis.zrange("zkey", 0, -1);
        System.out.println(counties);
    }


}
